<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/tools.js"></script>
    <style>
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }
        .container{
            padding-top: 80px;
            height: auto;
        }
        .container-right{
            width: auto;
        }
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
        .blog-pagnation-item{
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }
        .blog-pagnation-item:hover{
            background: #4e4eeb;
            color: #fff;
        }
        .blog-pagnation-item.actvie{
            background: #4e4eeb;
            color: #fff;
        }
    </style>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="myblog_draftlist.html">草稿页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="javascript:void(0)" onclick="onExit()">退出登陆</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img src="img/Pikachu.jpg" class="avtar" alt="">
                <h3 id="username">InvictusQAQ</h3>
                <a href="http:www.github.com">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span id="artcount"></span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div class="container-right"  style="width: 100%;">
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            <div class="blog" id="artlistDiv">
                
            </div>
            <hr>
            <div class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="firstClick()">首页</button> 
                <button class="blog-pagnation-item" onclick="beforeClick()">上一页</button>  
                <button class="blog-pagnation-item" onclick="nextClick()">下一页</button>
                <button class="blog-pagnation-item" onclick="lastClick()">末页</button>
            </div>
        </div>
    </div>
</body>

<script>
    //登录凭证
    var token=sessionStorage.getItem("token");

    var pindex = 1; //当前页码
        var psize = 3; //每页最多显示的条数信息
        var totalpage = 1; //总页数

        //初始化分页参数
        function initPageParam(){
            var pidx=getURLParam("pindex");
            if(pidx!=""){
                pindex=pidx;
            }
            var pz = getURLParam("psize");     
           if(pz!=""){
            psize=pz;
           }
        }
        initPageParam();

        //获取总页数
        function getTotalPage(){
            jQuery.ajax({
                url:"/art/mytotalpage",
                type:"GET",
                beforeSend: function(request) {
                    request.setRequestHeader("authorization",token);
                },
                data:{
                   "psize":psize
                },
                success:function(result){
                    if(result.code==200 && result.data>0){
                        totalpage=result.data; 
                    }
                }
            });
        }
        getTotalPage();

        var descLength=100;//简介最大长度
             // 首页
             function firstClick(){
            location.href = "myblog_list.html";
        }

        //截取正文简介
        function mySubstr(content){
            if(content.length>descLength){
                return content.substr(0,descLength)+"...";
            }
            return content;
        }

    //获取分页数据
    function getList(){
            jQuery.ajax({
                url:"/art/mylist",
                type:"GET",
                beforeSend: function(request) {
                    request.setRequestHeader("authorization",token);
                },
                data:{
                   "pindex":pindex,
                   "psize":psize
                },
                success:function(result){
                    //此人发表过文章
                if(result.code==200&&result.data!=null&&
                result.data.length>0){
                    var html="";
                    //foreach方法拼接文章
                    result.data.forEach(function(item){
                        html+='<div class="blog">'+
                                '<div class="title">'+item.title+'</div>'+
                                '<div class="date">'+item.createtime+'</div>'+'<div class="desc">'+
                                    mySubstr(item.content)+' </div>'+
                                    '<div style="text-align: center;margin-top: 50px;">'+
                                    '<a href="blog_content.html?id='+item.id+'">查看详情</a>&nbsp;&nbsp;'+
                                    '<a href="blog_update.html?id='+item.id+
                                    '">修改</a>&nbsp;&nbsp;<a href="javascript:myDel('+item.id
                                    +')">删除</a></div>'
                                    +'</div>';
                    });
                    jQuery("#artlistDiv").html(html);
                    }else{
                    //未发表过文章
                        jQuery("#artlistDiv").html("<h1>暂无数据</h1>");
                    }
                },
                error:function(xhr){
                    if(xhr.status==401){
                        alert("当前用户未登录，即将跳转至登录页");
                        location.href="/login.html";
                 }
             }
            });
        }
        getList();

        //上一页
        function beforeClick(){
            if(pindex<=1){
                alert("当前已在首页")
                return false;
            }
            pindex = parseInt(pindex)-1;
            location.href="myblog_list.html?pindex="+pindex+"&psize="+psize;
        }

        //下一页
        function nextClick(){
            if(pindex>=totalpage){
                alert("当前已在末页");
                return false;
            }
            pindex = parseInt(pindex)+1;
            location.href="myblog_list.html?pindex="+pindex+"&psize="+psize;
        }

        //末页
        function lastClick(){
            pindex = totalpage;
            location.href = "myblog_list.html?pindex="+pindex+"&psize="+psize;
        }

    // 获取用户信息
    function myInfo(){
        jQuery.ajax({
            url:"/user/myinfo",
            type:"POST",
            beforeSend: function(request) {
                request.setRequestHeader("authorization",token);
            },
            data:{},
            success:function(result){
                jQuery("#username").html(result.data.username);
                jQuery("#artcount").html(result.data.totalarticle);
            },
            err:function(){
                jQuery("username").html("加载用户信息异常");
                jQuery("artcount").html("加载用户信息异常");
            }
        });
    }
    myInfo();

    //删除文章
    function myDel(aid){
        if(confirm("是否确认删除该文章")){
                jQuery.ajax({
                url:"/art/artdel",
                type:"POST",
                beforeSend: function(request) {
                request.setRequestHeader("authorization",token);
                },
                data:{
                 aid:aid
                },
                success:function(result){
                    alert(result.data)
                    location.reload(true);
                },
            });
        }
    }
</script>

</html>